#!/usr/bin/python2.5 

import sys
sys.path.append("../../")

import time
from runtime import *
from game import *
from pygame.locals import *


"""
RE-WRITTEN:
* 6th Dec 2007 to make use of new object based scripting
* 29th Nov 2007 to use python scripting
"""




def create_pickled_game():
    """
    ############################################################################
    START OF GAME CODE
    """

    # Create a new Game
    mytestGame = Game("Catch the Puppy - GameBaker Demo")

    # Create a Game-Screen / Level
    mytestgamescreen = mytestGame.gamescreen((800,600),"screen1")

    # Load this screen first
    mytestGame.startscreen = "screen1"

    # Create a GameObject to show in the game - will later need a sprite attatched
    mytestGameObject = mytestGame.gameobject("test_object")

    # Say that we want this object to appear immediately in our game-screen
    mytestgamescreen.startobjects.append(mytestGameObject)


    # Create a score object
    score = mytestGame.gameobject("Score")
    mytestgamescreen.startobjects.append(score)



    # Set up a workstate for our object
    ws1 = mytestGame.workstate("WS1")

    ws1.actions[EVENT_INIT] = """
self.x=400
self.y=300
self.vx = int(random.random() * 75)
self.vy = int(random.random() * 75)
self.resize(45,45)
"""

    ws1.actions[EVENT_ITERATION] = """
self.move_velocity()
"""

    ws1.actions[EVENT_OFFSCREEN] = """
self.x=400
self.y=300
sign1 = random.random() - 0.5
sign2 = random.random() - 0.5
sign1 = sign1/abs(sign1)
sign2 = sign2/abs(sign2)
self.vx = sign1 * int(2 + random.random() * 75)
self.vy = sign2 * int(2 + random.random() * 75)

"""



    ws1.actions[EVENT_LMOUSEUP] = """
game.score = game.score + 1
"""


    # Tell the GameObject to start with this workstate
    mytestGameObject.baseworkstate = "WS1"

    wsscore = mytestGame.workstate("WSScore")

    wsscore.actions[EVENT_INIT] = """
self.x = 0 
self.y = 0
self.color = (255,255,255)
self.resize((60,20))
self.clear()
"""
    wsscore.actions[EVENT_ITERATION] = """

self.clear()
self.draw_text(game.score,20,(255,0,0))
"""
    score.baseworkstate = "WSScore"




    # Now Create the Puppy Sprite
    puppysprite = mytestGame.sprite("puppyspr")

    # The images are in one file, going downwards
    puppysprite.imagefiles.append({"filename":"images/puppy.bmp", "topleft":(0,0), "bottomright":(45,45)})
    puppysprite.imagefiles.append({"filename":"images/puppy.bmp", "topleft":(0,45), "bottomright":(45,90)})
    puppysprite.imagefiles.append({"filename":"images/puppy.bmp", "topleft":(0,90), "bottomright":(45,135)})
    puppysprite.imagefiles.append({"filename":"images/puppy.bmp", "topleft":(0,135), "bottomright":(45,180)})

    # update the sprite frame every two game frames
    puppysprite.framerate = 2
    puppysprite.alphakey = (0,255,0)

    # save this sprite to import later
    import gbfileio
    gbfileio.savegame("puppysprite.gbs",puppysprite,typ="sprite")

    #tell the main gameobject to use this
    mytestGameObject.sprite = "puppyspr"



    """
    *********************************************************************************
    END OF GAME CODE
    """
    import gbfileio
    gbfileio.savegame("testgame.gb",mytestGame)


    game = gbfileio.loadgame("testgame.gb")
    run_game(game)



if __name__ == "__main__":
    create_pickled_game()
